﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Win32;

namespace ProgramWindow.BLL
{
    class AppBLL:Win32BLL
    {
        static string formTitle = "Nuvoton NuMicro ICP Programming Tool 3.17";
        public static void CloseApp()
        {
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains("ICP Programming Tool"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
        }
        public static bool GetAPP(UIElement uielement)
        {
            try
            {
                CloseApp();
                Process process1 = new Process();
                process1.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "\\Nuvoton Tools\\ICPTool\\";
                process1.StartInfo.FileName = "NuMicro ICP Programming Tool.exe";
                process1.Start();
                Thread.Sleep(1000);
                bool waitOpen = true;
                while (waitOpen)
                {
                    waitOpen = false;
                    EnumDesktopWindows(IntPtr.Zero,
                         new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                         {
                             StringBuilder sb = new StringBuilder(256);
                             User.GetWindowText(hWnd, sb, sb.Capacity);
                             if (sb.ToString().Equals(formTitle))
                             {
                                 User.EnumChildWindows(hWnd, (hWnd1, lParam) =>
                                 {
                                     User.GetWindowText(hWnd1, sb, sb.Capacity);
                                     if (sb.ToString().Contains("继续>>"))
                                     {
                                         waitOpen = true;
                                     }
                                     return true;
                                 }, 0);
                             }
                             return true;
                         }),
                 IntPtr.Zero);
                }
                Thread.Sleep(2000);
                waitOpen = true;
                while (waitOpen)
                {
                    var  myproc = Process.GetProcesses();
                    if (myproc.Count(it => it.ProcessName.Contains("ICP Programming Tool")) == 0)
                    {
                        return false;
                    }
                    EnumDesktopWindows(IntPtr.Zero,
                         new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                         {
                             StringBuilder sb = new StringBuilder(256);
                             User.GetWindowText(hWnd, sb, sb.Capacity);
                             if (sb.ToString().Contains(formTitle))
                             {
                                 FormInsert(hWnd, uielement);
                                 waitOpen = false;
                             }
                             return true;
                         }),
                     IntPtr.Zero);
                }
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool CheckEditText(string text)
        {
            try
            {
                bool checkOk = false;
                EnumDesktopWindows(IntPtr.Zero,
                 new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                 {
                     StringBuilder sb = new StringBuilder(256);
                     User.GetWindowText(hWnd, sb, sb.Capacity);
                     if (sb.ToString().Contains("惠科烧录工具"))
                     {
                         User.EnumChildWindows(hWnd, (hWnd1, lParam) =>
                         {
                             User.GetClassName(hWnd1, sb, sb.Capacity);
                             if (sb.ToString().Contains("Edit"))
                             {
                                 // 取出窗口文本
                                 int textLen;
                                 textLen = SendMessage(hWnd1, User.WM_GETTEXTLENGTH, 0, 0);
                                 Byte[] byt = new Byte[textLen];
                                 SendMessage(hWnd1, User.WM_GETTEXT, textLen + 1, byt);
                                 string Text = Encoding.Default.GetString(byt);
                                 if (Text.Contains(text))
                                 {
                                     checkOk = true;
                                 }
                                 // 关闭Error窗口
                                 // SendMessage(h, WM_CLOSE , 0, 0);
                             }
                             return true;
                         }, 0);
                     }
                     return true;
                 }),
             IntPtr.Zero);
                return checkOk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool CheckText(string text)
        {
            try
            {
                bool checkOk = false;
                EnumDesktopWindows(IntPtr.Zero,
                 new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                 {
                     StringBuilder sb = new StringBuilder(256);
                     User.GetWindowText(hWnd, sb, sb.Capacity);
                     if (sb.ToString().Contains("惠科烧录工具"))
                     {
                         User.EnumChildWindows(hWnd, (hWnd1, lParam) =>
                         {
                             User.GetWindowText(hWnd1, sb, sb.Capacity);
                             if (sb.ToString().Contains(text))
                             {
                                 checkOk = true;
                             }
                             return true;
                         }, 0);
                     }
                     return true;
                 }),
             IntPtr.Zero);
                return checkOk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool GetCheckButton(string name)
        {
            try
            {
                bool checkOk = false;
                EnumDesktopWindows(IntPtr.Zero,
                    new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                    {
                        StringBuilder sb = new StringBuilder(256);
                        User.GetWindowText(hWnd, sb, sb.Capacity);
                        if (sb.ToString().Equals("惠科烧录工具"))
                        {
                            User.EnumChildWindows(hWnd, (hWnd1, lParam) =>
                            {
                                User.GetWindowText(hWnd1, sb, sb.Capacity);
                                if (sb.ToString().Contains(name))
                                {
                                    int state = SendMessage(hWnd1, User.BM_GETCHECK, 0, 0);
                                    if (state == 1)
                                    {
                                        checkOk = true;
                                    }
                                }
                                return true;
                            }, 0);
                        }
                        return true;
                    }),
             IntPtr.Zero);
                return checkOk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool ClickButton(string name)
        {
            try
            {

                IntPtr ParentHandle = (IntPtr)User.FindWindow(null, "惠科烧录工具");
                while (ParentHandle == IntPtr.Zero)
                    ParentHandle = (IntPtr)User.FindWindow(null, "惠科烧录工具");
                StringBuilder sb = new StringBuilder(256);
                User.EnumChildWindows(ParentHandle, (hWnd1, lParam) =>
                {
                    User.GetWindowText(hWnd1, sb, sb.Capacity);
                    if (sb.ToString().Contains(name))
                    {
                        Task.Factory.StartNew(() =>
                        {
                            SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                        });
                    }
                    return true;
                }, 0);
                //if (sb.ToString().Contains("NuMicro ICP Programming Tool"))
                //{
                //    User.EnumChildWindows(hWnd, (hWnd1, lParam) =>
                //    {
                //        User.GetWindowText(hWnd1, sb, sb.Capacity);
                //        if (sb.ToString().Equals("确定"))
                //        {
                //            SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                //        }
                //        if (sb.ToString().Equals("否(&N)"))
                //        {
                //            SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                //            isOk = true;
                //        }
                //        return true;
                //    }, 0);
                //}
                bool isOk = false;
                Task.Factory.StartNew(() =>
                {
                    while (isOk == false)
                    {
                        Thread.Sleep(500);
                        ParentHandle = (IntPtr)User.FindWindow(null, "NuMicro ICP Programming Tool");
                        while (ParentHandle == IntPtr.Zero)
                            ParentHandle = (IntPtr)User.FindWindow(null, "NuMicro ICP Programming Tool");
                        Thread.Sleep(500);
                        User.EnumChildWindows(ParentHandle, (hWnd1, lParam) =>
                        {
                            User.GetWindowText(hWnd1, sb, sb.Capacity);
                            if (sb.ToString().Contains("使用批量编程模式"))
                            {
                                isOk = true;
                            }
                            if (sb.ToString().Equals("否"))
                            {
                                SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                            }
                            if (sb.ToString().Equals("确定"))
                            {
                                SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                            }
                            if (sb.ToString().Equals("否(&N)"))
                            {
                                SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                            }
                            return true;
                        }, 0);
                    }
                    BaseTool.CommonHelper.Delay(500);
                }).Wait();
                return isOk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool Program()
        {
            try
            {
                bool isOk = false;
                bool wait = true;
                bool finish = false;
                Task.Factory.StartNew(() =>
                {
                    IntPtr ParentHandle = (IntPtr)User.FindWindow(null, "NuMicro ICP Programming Tool");
                    while (ParentHandle == IntPtr.Zero)
                        ParentHandle = (IntPtr)User.FindWindow(null, "NuMicro ICP Programming Tool");
                    while (wait)
                    {
                        Thread.Sleep(1000);
                        StringBuilder sb = new StringBuilder(256);
                        User.EnumChildWindows(ParentHandle, (hWnd1, lParam) =>
                        {
                            User.GetWindowText(hWnd1, sb, sb.Capacity);
                            if (sb.ToString().Contains("成功完成"))
                            {
                                isOk = true;
                            }
                            if (sb.ToString().Equals("确定"))
                            {
                                if (finish == true)
                                {
                                    SendMessage(hWnd1, User.BM_CLICK, 0, 0);
                                    wait = false;
                                }
                                finish = true;
                            }
                            return true;
                        }, 0);
                    }
                    BaseTool.CommonHelper.Delay(500);
                }).Wait();
                return isOk;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void FormInsert(IntPtr hwnd, UIElement uielement)
        {
            try
            {
                IntPtr hwnd1 = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle;
                User.SetParent(hwnd, hwnd1);
                User.SetWindowLong(hwnd, User.GWL_STYLE, User.WS_VISIBLE);
                User.MoveWindow(hwnd, 80, 55, 1150, 880, 1);
                Thread.Sleep(500);
                User.ShowWindow(hwnd, User.SW_SHOWDEFAULT);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
